package controllers;

import entities.Domain;
import entities.OwnerDomain;
import forms.OwnerDomainForm;
import helpers.StringHelper;
import helpers.UrlHelper;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import admin.models.DomainModel;
import admin.models.OwnerDomainModel;

import com.google.apphosting.utils.remoteapi.RemoteApiPb.Request;

public class ViewOwnerSiteServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		TreeMap check = new TreeMap();
		String username = request.getUserPrincipal().getName();
		
		OwnerDomainModel oModel = OwnerDomainModel.getInstance();
		ArrayList<OwnerDomain> vertifyDomain = oModel.getVertifyOwnerDomain(username);
		ArrayList<OwnerDomain> unvertifyDomain = oModel.getUnvertifyOwnerDomain(username);
		
		request.setAttribute("check", check);
		request.setAttribute("vertifyDomain", vertifyDomain);
		request.setAttribute("unvertifyDomain", unvertifyDomain);
		try {
			///demo/pages/view_owner_site.jsp 
			request.getRequestDispatcher("/pages/profile_view.jsp").forward(request, response);
		} catch (ServletException e) {}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		TreeMap check = new TreeMap();
		String username = request.getUserPrincipal().getName();
		
		OwnerDomainModel oData = OwnerDomainModel.getInstance();
		OwnerDomain ownerDomain = new OwnerDomain();
		ArrayList<OwnerDomain> vertifyDomain = oData.getVertifyOwnerDomain(username);
		ArrayList<OwnerDomain> unvertifyDomain = oData.getUnvertifyOwnerDomain(username);
		
		String yoursite = (String) request.getParameter("yoursite").replaceAll("(http://|https://)*", "");
		
		yoursite = UrlHelper.getMainDomain(yoursite);
		
		OwnerDomainForm oForm = new OwnerDomainForm();
		check = oForm.checkValidUrl(request);
		
		if (check.size() == 0) {
			
			ownerDomain.setDomain(yoursite);
			ownerDomain.setUsername(username);
			ownerDomain.setId();
			ownerDomain.setCodeVerify(StringHelper.md5(yoursite));
			if (oData.checkExists(ownerDomain.getId())) {
				response.sendRedirect("/profile/view");
				return;
			} else {
				
				DomainModel domainModel = DomainModel.getInstance();
				Domain domain = domainModel.get(yoursite);
				
				
				if (domain != null) {
					ownerDomain.setGoogleAnalyticPropertyId(domain.getGoogleAnalyticPropertyId());
					oData.insert(ownerDomain);
					response.sendRedirect("/profile/verify?domain=" + yoursite);
					return;
				} else {
					oData.insert(ownerDomain);
					response.sendRedirect("/process/" + yoursite + "?type=verify");
					return;
				}
			}
		}
		
		request.setAttribute("check", check);
		request.setAttribute("yoursite", yoursite);
		request.setAttribute("vertifyDomain", vertifyDomain);
		request.setAttribute("unvertifyDomain", unvertifyDomain);
		try {
			request.getRequestDispatcher("/pages/profile_view.jsp").forward(request, response);
		} catch (ServletException e) {}
	}
}
